package com.qianbei.user.income;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.init.SetData;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.share.ui.ShareFragmentDialog;

import java.util.ArrayList;

import static com.qianbei.common.init.SetData.getCareer;
import static com.qianbei.common.init.SetData.getFace;

public class MyIncomeActivity extends BaseActivity implements View.OnClickListener {

    private TabLayout mIncomeTabLayout;
    private ViewPager mIncomeVp;
    private Button mIncomeBt;
    private String userID;
    private TextView mTvIncomeMoney;
    private TextView mTvIncomePoint;
    private TextView mTvIncomeRate;
    private LinearLayout mLlMyIncome;
    private LinearLayout mLlNoEmptyView;
    private ImageView mIvNoNetWork;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_income);
        userID = SetData.getUserID();

        setTitle("我的收入");
        initView();

        initGetData();
        initEvent();
    }

    private void initGetData() {
        ServerControl sc = new ServerControl(Request.Method.GET, DETAIL_OF_INCOME + userID + "?");

        sc.serverListener = new ServerControl.ServerListener() {
            @Override
            public void serverFinish(ServerResult sr) {
                if (sr.isContinue) {
                    initEvent();
                    mLlMyIncome.setVisibility(View.VISIBLE);
                    mLlNoEmptyView.setVisibility(View.GONE);

                    IncomeBean incomeBean = JSON.parseObject(sr.bodyData.toString(), IncomeBean.class);

                    double money = incomeBean.getData().getMoney();
                    int point = incomeBean.getData().getPoint();
                    double rate = incomeBean.getData().getRate();

                    mTvIncomeMoney.setText("￥" + money);
                    mTvIncomePoint.setText("￥" + point);
                    mTvIncomeRate.setText("昨日金币转零钱汇率: " + rate);

                } else {
                    mLlMyIncome.setVisibility(View.GONE);
                    mLlNoEmptyView.setVisibility(View.VISIBLE);
                }
            }
        };
        sc.startVolley();
    }

    private void initEvent() {

        IncomeVpAdapter adapter = new IncomeVpAdapter(getSupportFragmentManager());
        ;
//        adapter.addFragment(new MoneyFragment(), "零钱");
//        adapter.addFragment(new PointFragment(), "金币")

        mIncomeVp.setAdapter(adapter);
        mIncomeVp.setOffscreenPageLimit(0);

        mIncomeTabLayout.setupWithViewPager(mIncomeVp);

//        mIncomeTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                switch (tab.getText().toString()) {
//                    case "金币":
//                        EventBus.getDefault().post(new MessageEvent("point"));
//                        break;
//                    case "零钱":
//                        EventBus.getDefault().post(new MessageEvent("money"));
//                        break;
//                }
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
    }

    public void initView() {
        mIncomeTabLayout = (TabLayout) findViewById(R.id.income_tabLayout);
        mIncomeVp = (ViewPager) findViewById(R.id.income_vp);
        mIncomeBt = (Button) findViewById(R.id.income_bt);
        mTvIncomeMoney = (TextView) findViewById(R.id.tv_income_money);
        mTvIncomePoint = (TextView) findViewById(R.id.tv_income_point);
        mTvIncomeRate = (TextView) findViewById(R.id.tv_income_rate);
        mLlMyIncome = (LinearLayout) findViewById(R.id.ll_my_income);
        mLlNoEmptyView = (LinearLayout) findViewById(R.id.ll_no_emptyview);
        mIvNoNetWork = (ImageView) findViewById(R.id.iv_no_network);

        mIvNoNetWork.setOnClickListener(this);
        mIncomeBt.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_no_network:
                initGetData();
                break;
            case R.id.income_bt:
                ArrayList<Integer> menuItem = new ArrayList<Integer>();
                menuItem.add(R.string.share_webo);
                menuItem.add(R.string.share_wei_xin);
                menuItem.add(R.string.share_friend);
                menuItem.add(R.string.cancel);


                ShareFragmentDialog dialogs = ShareFragmentDialog.newInstance("我是" + SetData.getUserName() + "(" + getCareer() + ")" + "来「前辈」向我提问", "前辈 — 向专业人士提问，获取一对一个性化答案。", String.format(PERSONAL_URL,
                        SetData.getUserID()), URL + getFace(), "web",false);
                dialogs.show(getFragmentManager(), this.getClass().toString());
                break;
        }
    }


//    @Override
//    public void onDestroy() {
//        super.onDestroy();
//        EventBus.getDefault().unregister(this);
//    }
//
//    @Override
//    public void onStart() {
//        super.onStart();
//        EventBus.getDefault().register(this);
//
//    }
}
